package cn.edu.ujn.food.controller;

import cn.edu.ujn.food.entity.*;
import cn.edu.ujn.food.service.*;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Controller
public class OrderController {
    @Autowired
    private UserService userService;
    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private DishService dishService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderdetailService orderdetailService;
    @ModelAttribute("userMap")
    public Map<Integer, String> getUserMap() {
        List<User> users = userService.findAll();
        Map<Integer, String> userMap = new HashMap<>();
        for (User user : users) {
            userMap.put(user.getUserId(), user.getUserName());
        }
        return userMap;
    }
    @ModelAttribute("restaurantMap")
    public Map<Integer, String> getRestaurantMap() {
        List<Restaurant> restaurants = restaurantService.findAll();
        Map<Integer, String> restaurantMap = new HashMap<>();
        for (Restaurant restaurant : restaurants) {
            restaurantMap.put(restaurant.getRestId(), restaurant.getRestName());
        }
        return restaurantMap;
    }

    @ModelAttribute("dishMap")
    public Map<Integer, String> getDishMap() {
        List<Dish> dishes = dishService.findAll();
        Map<Integer, String> dishMap = new HashMap<>();
        for (Dish dish : dishes) {
            dishMap.put(dish.getDishId(), dish.getDishName());
        }
        return dishMap;
    }

    @GetMapping("/order/{id}")
    @ResponseBody
    public ResponseEntity<Order> getOrderById(@PathVariable("id") Integer orderId) {
        Order order = orderService.queryById(orderId);
        if (order == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(order, HttpStatus.OK);
    }

    @PutMapping("/order/{id}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> updateOrder(@PathVariable("id") Integer orderId, @RequestBody Order order) {
        try {
            order.setOrderId(orderId);
            orderService.update(order);
            return ResponseEntity.ok(Map.of("success", true, "message", "订单信息已成功更新"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/order/{id}")
    public ResponseEntity<Map<String, Object>> deleteOrder(@PathVariable("id") Integer orderId) {
        try {
            boolean isDeleted = orderService.deleteById(orderId);
            if (isDeleted) {
                return ResponseEntity.ok(Map.of("success", true, "message", "订单信息删除成功！"));
            } else {
                return new ResponseEntity<>(Map.of("success", false, "message", "订单信息删除失败，订单不存在或已删除。"), HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(Map.of("success", false, "message", "订单信息删除失败: " + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/order")
    @ResponseBody
    public String createOrder(Order order) {
        try {
            orderService.insert(order);
            return "OK";
        } catch (Exception e) {
            return "FAIL";
        }
    }

    @GetMapping("/order")
    public String listOrder(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Order order,Orderdetail orderdetail) {
        order.setStart((page - 1) * rows);
        order.setRows(rows);
        Page<Order> page6 = orderService.selectAll(order);
        page6.setPage(page);
        model.addAttribute("page1", page6);

        orderdetail.setStart((page - 1) * rows);
        orderdetail.setRows(rows);
        Page<Orderdetail> page7 = orderdetailService.selectAll(orderdetail);
        page7.setPage(page);
        model.addAttribute("page2", page7);
        return "order";
    }
}


